$(function (){

let layer = layui.layer
let slider = layui.slider
init()

async function init(){
    const serect = await wxSDK()
    await SDKyanzheng(serect)
    await anonymouslogin()
        let loading =  layer.load(2)
        let listi = await list();
        let data = listi;
        let $li = ``
        for(let i = 0; i < data.length ; i++){
            let item = data[i]
            let date = item['_id'].replace(/[A-Z a-z]/g,"")
            if(i==0){
                $li =`<li class="list-group-item list-group-item-action hyitem" data-id="${item['_id']}">
                    <div class="w-50 text-truncate float-left">${item['biaoTi']}</div>
                    <div class="small float-right">
                    <span class="badge badge-danger badge-pill">最新</span>
                    </div>
                  </li>`
            }else{
                $li =`<li class="list-group-item list-group-item-action hyitem" data-id="${item['_id']}">
                    <div class="w-50 text-truncate float-left">${item['biaoTi']}</div>
                    <div class="small float-right">${date}</div>
                  </li>`
            }

            $("#list").append($li)
        }

        $('audio').attr('src',BASEHOST+'/huayu/dmt/'+data[0]['_id']+'.mp3')

        layer.close(loading)

        $(".hyitem").on('click',async function (e){
            $(".playPage").animate({
                left:'50%'
            })
            showPlayPlne()
            hideplaycon()
            let currentItem = data.filter(item=>item['_id'] == this.dataset['id'])
            console.log(currentItem);
            let imgUrl = currentItem[0]['dmtUrl']['gepuUrl']
            let audioUrl = currentItem[0]['dmtUrl']['adUrl']
            let biaoTi = currentItem[0]['biaoTi']
            let loading = layer.load(2)

            $("#hyImg").attr('src',imgUrl).load(()=>{
                layer.close(loading)
            }).error(()=>{
                layer.msg("图片请求失败")
                $("#hyImg").attr('src','./img/kong.png')
                layer.close(loading)
            })
            if(audioUrl == $('audio').attr('src')){
                return
            }
            //如果是微信浏览器
            if(iswxBrower()){
                $('#play').hide()
                $('#wxaudio').attr('src',audioUrl)
                $('#wxaudio').attr('title',biaoTi)
                $('#wxaudio').attr('singer','讲台信息')
                $('#wxaudio').attr('episode',' ')
                $('#wxaudio').attr('cover','https://7367-sgb-8gqmg7kv48722f4e-1306391227.tcb.qcloud.la/sgb/ad/ee0c24590f9857dc1a225e7f0a88a94.jpg')

            } else {
                $('#play').show()
                $('#play')[0].className = 'iconfont icon-bofang'
                $('audio').attr('src',audioUrl).load(()=>{
                }).error(()=>{
                    layer.msg('音频请求失败')
                })
            }
        })

        $("#close").click(()=>{
            $(".playPage").animate({left:'-50%'})
            $("#hyImg").unbind()
            $("#hyImg").attr('src',"")
        })

        $('#play').click(function (){
            console.log(this.className);
            if(this.className=='iconfont icon-bofang'){
                if($('audio').attr('src')==""){
                    layer.msg('当前没有要播放的音频')
                    return
                }

                this.className = 'iconfont icon-pause-full'
                $('audio')[0].play()
            }else{
                this.className = 'iconfont icon-bofang'
                $('audio')[0].pause()
            }
        })

        $('audio')[0].onplay = function(){

            $('#play')[0].className = 'iconfont icon-pause-full'
        }

        $('audio')[0].onpause = function(){

            $('#play')[0].className = 'iconfont icon-bofang'

        }

        $('audio')[0].ontimeupdate = function(e){

            widthCss($("#proressCnl")[0],$('audio')[0].currentTime / $('audio')[0].duration * 100)
        }

        $('audio')[0].onended = function(e){

            widthCss($("#proressCnl")[0],0)

            $('#play')[0].className = 'iconfont icon-bofang'

        }

        $("#beizu").click(function (){

            let bs = parseInt(this.dataset['bs'])
            let arr = [1,1.5,2]
            bs+=1
            if(iswxBrower()){
                $('#wxaudio')[0].playbackRate = arr[bs%3];//设置速度1/1.5/2
            } else {
                $('audio')[0].playbackRate = arr[bs%3];//设置速度1/1.5/2
            }

            $(this).attr('data-bs',bs).text("倍速X"+arr[bs%3])

        })

        $("#playPlanHide").click(()=>{

            hidePlayPlne()
            showplaycon()

        })

        $(".showPlayPlan").click(()=>{
            showPlayPlne()
            hideplaycon()
        })

        //进度条拖拽事件监听
        dragProgress($("#progress")[0],$("#proressCnl")[0],

            function(val){ //开始

                var b = $('audio')[0].paused //获取audio的初始状态
                this.stPaused = b
                if(!b){
                    $('audio')[0].pause()
                }

            },
            function(val){ //移动
                console.log(val)
            },
            function(val){ //结束

                var a = $('audio')[0].duration * (val/100)

                $('audio')[0].currentTime = a
                if(!this.stPaused){
                    $('audio')[0].play()
                }

            }
        )



}

//初始化获取list
function list (){
    return new Promise((resolve, reject) => {
        HYDB.gitZRList().then(res=>{
          resolve(res)
        }).catch(err=>{
            console.log(err);
        })
    })
}


//隐藏播放页面显示按钮
function hideplaycon(){
    $('.showPlayPlan').animate({
        left:'-54px'
    })
}

//隐藏播放页面显示按钮
function showplaycon(){
    $('.showPlayPlan').animate({
        left:'10px'
    })
}

//隐藏播放页面板
function hidePlayPlne(){
    $(".playPlan").animate({
        bottom:'-80px'
    })
}

//显示播放页面板
function showPlayPlne(){
    $(".playPlan").animate({
        bottom:'0px'
    })
}


})
